module.exports = function(e) {
    var t = require("../util"), i = null;
    return t.ext(e.prototype, {
        addHook: function() {
            return this.isHookInstantiated ? this : (function() {
                var e = this;
                if ("undefined" != typeof my && my && "function" == typeof my.request) {
                    var s = my.request;
                    i = s;
                    var a = Object.getOwnPropertyDescriptor(my, "request");
                    a && a.writable && (my.request = function(i) {
                        var a = new Date().getTime(), n = i;
                        if (i && i.url) {
                            var r = t.cutUrlSearch(i.url);
                            if (!t.checkAPI(r, !0)) return s.call(my, n);
                            var o, c, u = n && n.headers;
                            u && "object" == typeof u || (u = {});
                            var f = {};
                            if (e.getConfig("enableLinkTrace")) {
                                var l = u["EagleEye-pAppName"];
                                if (o = u["EagleEye-TraceID"], c = u["EagleEye-SessionID"], o || (o = e.getTraceId()["EagleEye-TraceID"], 
                                f["EagleEye-TraceID"] = o), c || (c = e.getPageviewId()["EagleEye-SessionID"], f["EagleEye-SessionID"] = c), 
                                !l) {
                                    var y = e.getConfig("pid");
                                    f["EagleEye-pAppName"] = y;
                                }
                            }
                            var d = {
                                success: function(t) {
                                    "function" == typeof i.success && i.success(t);
                                    var s = new Date().getTime();
                                    e.api({
                                        api: r,
                                        success: !0,
                                        time: s - a,
                                        code: t && t.status || 200,
                                        begin: a,
                                        traceId: o,
                                        pv_id: c
                                    });
                                },
                                fail: function(t) {
                                    "function" == typeof i.fail && i.fail(t);
                                    var s = new Date().getTime(), n = "";
                                    t && t.status && (n = t.status);
                                    var u = "";
                                    t && t.body && (u = (u = JSON.stringify(t.body)).substring(0, 1e3)), e.api({
                                        api: r,
                                        success: !1,
                                        time: s - a,
                                        code: n,
                                        msg: u,
                                        begin: a,
                                        traceId: o,
                                        pv_id: c
                                    });
                                },
                                headers: t.ext({}, u, f)
                            };
                            n = t.ext({}, n, d);
                        }
                        return s.call(my, n);
                    });
                }
            }.call(this), this.isHookInstantiated = !0, this);
        },
        removeHook: function() {
            return this.isHookInstantiated ? (function() {
                if ("undefined" != typeof my && my && i) {
                    var e = Object.getOwnPropertyDescriptor(my, "request");
                    e && e.writable && (my.request = i), i = null;
                }
            }.call(this), this.isHookInstantiated = !1, this) : this;
        },
        initHook: function() {
            return this.hasInitHook ? this : (this.getConfig("disableHook") || this.addHook(), 
            this.hasInitHook = !0, this);
        }
    }), e;
};